﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using TracNghiem.Areas.Admin.Models;
using System.Collections;

namespace TracNghiem.Areas.Admin.Controllers
{
    //[Authorize(Roles = "admin")]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/Admin/
        AccountModel AccountModel = new AccountModel();
        public ActionResult Index()
        {
            if (!Request.IsAuthenticated || !User.IsInRole("admin"))
                return RedirectToAction("Login");
            
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(AccountModel Account)
        {
            //var errors = ModelState.Values.SelectMany(v => v.Errors);
            if (ModelState.IsValid)
            {
                if (Account.username == "Admin" && AccountModel.checkAccount(Account) > 0)
                {
                    FormsAuthentication.SetAuthCookie(Account.username, Account.RememberMe);
                    return RedirectToAction("Index");
                }
            }
            TempData["msg"] = "Tên đăng nhập hoặc mật khẩu không chính xác!";
            return View();
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            Session.Clear();
            //Session.Abandon();
            return RedirectToAction("Index");
        }

    }
}
